/* 
 *   Classname:  com.zzz.db.mssql.ParseScript
 * 
 *   Date       Developer   Description
 *   2018年1月15日    周成功              新建
 *
 */
package demo.database.mssql;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.titan.core.utils.data.FormatUtils;
import com.titan.core.utils.data.StringUtils;
import com.titan.core.utils.file.FileUtils;

/**
 *
 *
 * @author 周成功
 *
 */
public class ParseScript {

    public static void main(String[] args) {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void run() throws Exception {
        File input = new File("c:\\users\\zzz\\desktop\\script.sql");
        List<String> lines = FileUtils.toLines(input, "unicode");
        
//        List<String> cache = new ArrayList<String>();
        StringBuilder buff = new StringBuilder(1024);
        String title = null;
        for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i);
            if(line.startsWith("/******") && line.endsWith("******/")) {
                write(title, buff);
                title = line.substring(17, line.indexOf("    Script Date:"));
                buff.setLength(0);
//                System.out.println(title);
            } else {
                buff.append(line).append("\r\n");
            }
        }
        write(title, buff);
        
//        for (int i = 0; i < cache.size(); i++) {
//            String line = cache.get(i);
//            System.out.println("["+FormatUtils.to2Digit(i)+"]"+line);
//        }
    }
    
    
    private static void write(String title, StringBuilder context) throws IOException {
        if(StringUtils.isEmpty(title) || context.length()==0) {
            return;
        }
        
        String filePath = "E:\\sql";
        String fileName = title + ".sql";
        FileUtils.write(filePath, fileName, context.toString(), "UTF-8", false);
    }
}
